<?php
require ('../include/init.inc.php');
$tb_id = $tb_date = $tb_type = $tb_provide_name = $tb_provide_id = $tb_order_id = $tb_cost_photo = $tb_remark = '';
extract ( $_REQUEST, EXTR_IF_EXISTS );

Common::checkParam($tb_id);
$tb_info = TravelBill::getTravelBillById($tb_id);
if(empty($tb_info)){
	Common::exitWithError(ErrorMessage::USER_NOT_EXIST,"panel/travel_bill.php");
}
if (Common::isPost ()) {
    
    $pic = $_FILES['tb_cost_photo']['tmp_name'];
    $upload_ret = false;
    if($pic){
        // 上传的路径
        $photoDir = $_SERVER['DOCUMENT_ROOT']."/photos/travel/travelBill";
        // 创建文件夹
        if(!file_exists($photoDir)){
            mkdir($photoDir, 0777);
        }
        // 用时间戳来保存图片，防止重复
        $timeNow = time();
        $targetFile = $photoDir . '/' . $timeNow.'.jpg';
        $sqlFile = "/photos/travel/travelBill/".$timeNow.'.jpg';
        echo $targetFile."<br / >";
        // 将临时文件 移动到我们指定的路径，返回上传结果
        $upload_ret = move_uploaded_file($pic, $targetFile) ? true : false;
    }
    
	if($tb_id==0){
		
		OSAdmin::alert("error",ErrorMessage::NEED_PARAM);
	}else{
		
		$update_data = array ('tb_id' => $tb_id, 'tb_date' => $tb_date, 'tb_type' => $tb_type, 'tb_provide_name' => $tb_provide_name, 
		    'tb_provide_id' => $tb_provide_id, 'tb_order_id' => $tb_order_id, 'tb_remark' => $tb_remark
		);
		
		if($upload_ret == true){
		    $update_data = array_merge ( $update_data, array ('tb_cost_photo' => $sqlFile ));
		}
		
		$result = TravelBill::updateTravelBill ( $tb_id,$update_data );
		if ($result>=0) {
			$current_user=UserSession::getSessionInfo();
			$ip = Common::getIp();
			$update_data['ip']=$ip;
			SysLog::addLog ( UserSession::getUserName(), 'MODIFY', 'User' , $user_id, json_encode($update_data) );
			Common::exitWithSuccess ('更新完成','panel/travel_bill.php');
		} else {
			
			OSAdmin::alert("error");
		}
	}
}

$group_options=UserGroup::getGroupForOptions();
Template::assign ( 'type_list', $baseConf["optionList"] );
Template::assign ( 'tb_info', $tb_info );
Template::assign ( 'group_options', $group_options );
Template::display ( 'panel/travel_bill_modify.tpl' );